﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Globalization;

namespace fes
{
  public class StockData
  {
    private int it = -1;
    private String symbol;
    private List<string> rawData = new List<string>();
    private List<DateTime> date = new List<DateTime>();
    private List<double> open = new List<double>();
    private List<double> high = new List<double>();
    private List<double> low = new List<double>();
    private List<double> close = new List<double>();
    private List<int> vol = new List<int>();

    public bool AddData(DateTime d, double o, double h, double l, double c, int v) 
    {
      //TODO: porownanie list, o co chodzi?
        //if (high < low) return false;

        //rawData.Add(date.ToString();
        date.Add(d);
        open.Add(o);
        high.Add(h);
        low.Add(l);
        close.Add(c);
        vol.Add(v);

        return true;
    }

    public bool CutDataHead(int howMany) 
    {
        if (howMany > this.getSize()) 
        {
            return false;
        }

        if(open.Count>=howMany) open.RemoveRange(0, howMany);
        if (close.Count >= howMany) close.RemoveRange(0, howMany);
        if (high.Count >= howMany) high.RemoveRange(0, howMany);
        if (low.Count >= howMany) close.RemoveRange(0, howMany);
        if (date.Count >= howMany) date.RemoveRange(0, howMany);
        if (vol.Count >= howMany) vol.RemoveRange(0, howMany);
        if (rawData.Count >= howMany) rawData.RemoveRange(0, howMany);
        return true;
    }

    public bool readFromFile(String path)
    {
      try
      {
        StreamReader sr = new StreamReader(path);
        sr.ReadLine();  //skip first line
        NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;
        nfi.CurrencyDecimalSeparator = ".";

        StreamReader symbolReader = new StreamReader(path);
        symbolReader.ReadLine();
        string line = symbolReader.ReadLine();
        string[] v = line.Split(',');
        symbol = v[0];

        while (!sr.EndOfStream)
        {
          line = sr.ReadLine();
          rawData.Add(line);
          v = line.Split(',');
          v[1] = v[1].Insert(4, "-");
          v[1] = v[1].Insert(7, "-");
          date.Add(DateTime.Parse(v[1], nfi));
          open.Add(Double.Parse(v[2], nfi));
          high.Add(Double.Parse(v[3], nfi));
          low.Add(Double.Parse(v[4], nfi));
          close.Add(Double.Parse(v[5], nfi));
          double vl = Double.Parse(v[6],nfi);
          vol.Add((Int32) vl);
        };
      }
      catch (Exception e)
      {
        Console.WriteLine(e.Message);
        return false;
      }
      return true;
    }

    public int getNext()
    {
      it++;
      if (it < date.Count)
      {
        return it;
      }
      return -1;
    }

    public void Rewind()
    {
      it = -1;
    }

    public DateTime getDate()
    {
      if (it != -1 && it < date.Count)
      {
        return date[it];
      }
      return DateTime.MinValue;
    }

    public double getOpen()
    {
      if (it != -1 && it < open.Count)
      {
        return open[it];
      }
      return double.NaN;
    }

    public double getHigh()
    {
      if (it != -1 && it < high.Count)
      {
        return high[it];
      }
      return double.NaN;    
    }

    public double getLow()
    {
      if (it != -1 && it < low.Count)
      {
        return low[it];
      }
      return double.NaN;
    }

    public double getClose()
    {
      if (it != -1 && it < close.Count)
      {
        return close[it];
      }
      return double.NaN;
    }

    public int getVol()
    {
      if (it != -1 && it < vol.Count)
      {
        return vol[it];
      }
      return Int32.MinValue;
    }

    //------------------------------------------------------------
      //
      // overloaded methods to get the same data for n-dates later
      //
      //
    //----------------------

    public DateTime getDate(int n)
    {
        if (it != -1 && (it+n) < date.Count)
        {
            return date[it+n];
        }
        return DateTime.MinValue;
    }

    public double getOpen(int n)
    {
        if (it != -1 && it + n < open.Count)
        {
            return open[it + n];
        }
        return double.NaN;
    }

    public double getHigh(int n)
    {
        if (it != -1 && (it + n) < high.Count)
        {
            return high[it + n];
        }
        return double.NaN;
    }

    public double getLow(int n)
    {
        if (it != -1 && (it+n) < low.Count)
        {
            return low[it + n];
        }
        return double.NaN;
    }

    public double getClose(int n)
    {
        if (it != -1 && (it + n) < close.Count)
        {
            return close[it + n];
        }
        return double.NaN;
    }

    public int getVol(int n)
    {
        if (it != -1 && (it + n) < vol.Count)
        {
            return vol[it + n];
        }
        return Int32.MinValue;
    }

    public double getLastClose()
    {
      return close[close.Count - 1];
    }

    public double getCloseForDay(int n)
    {
      if (n >= 0 && n < close.Count)
      {
        return close[n];
      }
      return double.NaN;
    }
//----------------------------------------------------------------------------
    public DateTime getFirstDate()
    {
      return date[0];  
    }

    public DateTime getLastDate()
    {
      return date[date.Count - 1];
    }

    public int getSize()
    {
      return close.Count;
    }

    public string getSymbol()
    {
      return symbol;
    }


    //------------------------------------------------------------------------------

    public DateTime getDateToLast(int n)
    {
      if (date.Count > n)
      {
        return date[date.Count - 1 - n];
      }
      return DateTime.MinValue;
    }

    public double getOpenToLast(int n)
    {
      if (open.Count > n)
      {
        return open[open.Count - 1 - n];
      }
      return double.NaN;
    }

    public double getHighToLast(int n)
    {
      if (high.Count > n)
      {
        return high[high.Count - 1 - n];
      }
      return double.NaN;
    }

    public double getLowToLast(int n)
    {
      if (low.Count > n)
      {
        return low[low.Count - 1 - n];
      }
      return double.NaN;
    }

    public double getCloseToLast(int n)
    {
      if (close.Count > n)
      {
        return close[close.Count - 1 - n];
      }
      return double.NaN;
    }

    public int getVolToLast(int n)
    {
      if (vol.Count > n)
      {
        return vol[vol.Count - 1 - n];
      }
      return Int32.MinValue;
    }



  }

}
